#include <bits/stdc++.h>

using namespace std;

const int MAXN = 4e5+10;

int n,m,fa[MAXN],h[MAXN],a[MAXN],cnt,in[MAXN];

typedef struct {int v,c,nxt;}edge;
typedef struct {int v,nxt;}eee;

edge e[MAXN];

void link(int x,int y,int c){e[++cnt] = (edge){y,c,h[x]};h[x] = cnt;}

int gf(int x){return fa[x] == x?x:fa[x] = gf(fa[x]);}

void dfs(int x,int p){
	for(int i = h[x];i;i = e[i].nxt){
		int y = e[i].v;
		if(y == p) continue;
		if(a[x] == e[i].c){
			a[y] = a[x] % n + 1;
		}
		else a[y] = e[i].c;
		dfs(y,x);
	}
}

int main (){
	scanf("%d %d",&n,&m);
	for(int i = 1;i <= n;i++) fa[i] = i;
	for(int i = 1;i <= m;i++){
		int x,y,c;
		scanf("%d %d %d",&x,&y,&c);
		if(gf(x) != gf(y)){
			link(x,y,c);link(y,x,c);
			fa[gf(x)] = gf(y);;
		}
	}
	a[1] = 1;
	dfs(1,0);
	for(int i = 1;i <= n;i++) printf("%d\n",a[i]);
	return 0;
}
			
